import { defHttp } from '/@/utils/http/axios';
import {
  GeneratorData,
  GeneratorListGetResultModel,
  GeneratorParams
} from '/@/model/development/generatorModel';

enum Api {
  GET_GENERATOR_PAGE = '/gen/generator/page',
  GENERATOR_PREVIEW = '/gen/generator/preview',
  GET_GENERATOR_DS_PAGE = '/gen/dsconf/page',
  GET_GENERATOR_DS_LIST = '/gen/dsconf/list',
  GENERATOR_BASE = '/gen/dsconf/',
  GENERATOR_CODE = '/gen/generator/code',
}

export const fetchList = (params: GeneratorParams) =>
  defHttp.get<GeneratorListGetResultModel>(
    {url: Api.GET_GENERATOR_PAGE,params },
    { errorMessageMode: 'message' }
  );

export const preview = (params: {}) =>
  defHttp.get<any>(
    {url: Api.GENERATOR_PREVIEW,params },
    { errorMessageMode: 'message' }
  );

export const fetchDsList = (params: {}) =>
  defHttp.get<any>(
    {url: Api.GET_GENERATOR_DS_PAGE,params },
    { errorMessageMode: 'message' }
  );

export const fetchSelectDsList = (params: {}) =>
  defHttp.get<any>(
    {url: Api.GET_GENERATOR_DS_LIST,params },
    { errorMessageMode: 'message' }
  );

export const addObj = (data: {}) =>
  defHttp.post<any>(
    {url: Api.GENERATOR_BASE,data },
    { errorMessageMode: 'message' }
  );

export const getObj = (id) =>
  defHttp.get<any>(
    {url: Api.GENERATOR_BASE+'/'+id },
    { errorMessageMode: 'message' }
  );

export const delObj = (id) =>
  defHttp.delete<any>(
    {url: Api.GENERATOR_BASE+'/'+id },
    { errorMessageMode: 'message' }
  );

export const putObj = (data : GeneratorData) =>
  defHttp.put<any>(
    {url: Api.GENERATOR_BASE,data },
    { errorMessageMode: 'message' }
  );

export const handleDown = (data:{}) =>
  defHttp.post<any>(
    {url: Api.GENERATOR_CODE,data,responseType: 'arraybuffer' },
    { errorMessageMode: 'message', isTransformResponse: false }
  ).then((response) => { // 处理返回的文件流
    const blob = new Blob([response], { type: 'application/zip' })
    const filename = data.tableName + '.zip'
    const link = document.createElement('a')
    link.href = URL.createObjectURL(blob)
    link.download = filename
    document.body.appendChild(link)
    link.click()
    window.setTimeout(function () {
      URL.revokeObjectURL(blob)
      document.body.removeChild(link)
    }, 0)
  })




